﻿using Microsoft.Xna.Framework;  
using System;
using System.Collections.Generic;

namespace Project1
{
    public class PickupDescriptor
    {
        public Func<Pickup> Placer;
        public float Interval;
        public float LastPlaced;

        public Pickup PlacedPickup;

        public PickupDescriptor(Func<Pickup> placer, float interval, float lastPlaced)
        {
            Placer = placer;
            Interval = interval;
            LastPlaced = lastPlaced;

            PlacedPickup = placer();
        }
    }

    public class PickupFactory
    {
        List<PickupDescriptor> spawnPoints = new List<PickupDescriptor>();
        
        public PickupFactory() { }

        public void AddPickup(Func<Pickup> placer, float interval)
        {
            spawnPoints.Add(new PickupDescriptor(placer, interval, 0.0f));
        }

        public void Update(GameTime gametime)
        {
            foreach (PickupDescriptor desc in spawnPoints)
            {
                // If there is no pickup at this location and we haven't spawned
                // one in a while, ...
                if (desc.PlacedPickup.Dead &&
                    desc.LastPlaced < gametime.TotalGameTime.TotalSeconds - desc.Interval)
                {
                    desc.PlacedPickup = desc.Placer();
                    desc.LastPlaced = (float)gametime.TotalGameTime.TotalSeconds;
                }
            }
        }
    }
}
